package com.ruoyi.mes.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.mes.domain.*;
import com.ruoyi.mes.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.mes.service.IScheduleService;
import com.ruoyi.mes.vo.schedule.ScheduleVo;
/**
 * 生产排程Service业务层处理
 * 
 * @author LongAndHao
 * @date 2024-11-04
 */
@Service
public class ScheduleServiceImpl implements IScheduleService 
{
    @Autowired
    private ScheduleMapper scheduleMapper;
    @Autowired
    private WkspTaskMapper wkspTaskMapper;
    @Autowired
    private ProOrderMapper proOrderMapper;
    @Autowired
    private ProgPlanDetailMapper progPlanDetailMapper;
    @Autowired
    private ProgPlanMapper progPlanMapper;
    @Autowired
    private ProductionLineMapper productionLineMapper;
    /**
     * 查询生产排程
     * 
     * @param scheduleId 生产排程主键
     * @return 生产排程
     */
    @Override
    public Schedule selectScheduleByScheduleId(Long scheduleId)
    {
        return scheduleMapper.selectScheduleByScheduleId(scheduleId);
    }

    /**
     * 查询生产排程列表
     *
     * @param schedule 生产排程
     * @return 生产排程（剔除存在空值的记录）
     */
    @Override
    public List<Schedule> selectScheduleList(Schedule schedule) {
        List<Schedule> scheduleList = scheduleMapper.selectScheduleList(schedule);
        List<Schedule> resultList = new ArrayList<>();

        for (Schedule schedule1 : scheduleList) {
            // 获取任务对应的数据，并进行空值判断
            WkspTask wkspTask = wkspTaskMapper.selectWkspTaskByProgDetailId(schedule1.getTaskId());
            if (wkspTask == null) {
                continue; // 若wkspTask为空，则跳过此记录
            }

            // 调用selectWkspTaskById仅调用一次，避免重复查询
            WkspTask wkspTaskDetail = wkspTaskMapper.selectWkspTaskById(schedule1.getTaskId());
            if (wkspTaskDetail == null || wkspTaskDetail.getWkspCode() == null
                    || wkspTaskDetail.getWkspPlanQuantity() == null
                    || wkspTaskDetail.getWkspReadyQuantity() == null) {
                continue;
            }

            // 获取生产线信息
            ProductionLine productionLine = productionLineMapper.selectProductionLineByLineId(schedule1.getLineId());
            if (productionLine == null || productionLine.getLineName() == null) {
                continue;
            }

            // 获取计划明细信息
            ProgPlanDetail progPlanDetail = progPlanDetailMapper.selectProgPlanDetailById(wkspTask.getProgDetailId());
            if (progPlanDetail == null || progPlanDetail.getDateTime() == null) {
                continue;
            }

            // 设置各属性
            schedule1.setTaskName(wkspTaskDetail.getWkspCode());
            schedule1.setLineName(productionLine.getLineName());
            schedule1.setPlanNum(wkspTaskDetail.getWkspPlanQuantity());
            schedule1.setReadyNum(wkspTaskDetail.getWkspReadyQuantity());
            schedule1.setDateTime(progPlanDetail.getDateTime());

            // 将数据完整的记录加入返回列表
            resultList.add(schedule1);
        }
        return resultList;
    }


    /**
     * 新增生产排程
     * 
     * @param schedule 生产排程
     * @return 结果
     */
    @Override
    public int insertSchedule(Schedule schedule)
    {
        return scheduleMapper.insertSchedule(schedule);
    }

    /**
     * 修改生产排程
     * 
     * @param schedule 生产排程
     * @return 结果
     */
    @Override
    public int updateSchedule(Schedule schedule)
    {
        return scheduleMapper.updateSchedule(schedule);
    }

    /**
     * 批量删除生产排程
     * 
     * @param scheduleIds 需要删除的生产排程主键
     * @return 结果
     */
    @Override
    public int deleteScheduleByScheduleIds(Long[] scheduleIds)
    {
        return scheduleMapper.deleteScheduleByScheduleIds(scheduleIds);
    }

    /**
     * 删除生产排程信息
     * 
     * @param scheduleId 生产排程主键
     * @return 结果
     */
    @Override
    public int deleteScheduleByScheduleId(Long scheduleId)
    {
        return scheduleMapper.deleteScheduleByScheduleId(scheduleId);
    }
    @Override
    public List<ScheduleVo> selectScheduleByScheduleLineId(Long lineId) {
        // 结果列表
        List<ScheduleVo> scheduleVoList = new ArrayList<>();

        // 如果 lineId 为空，直接返回空列表
        if (lineId == null) {
            return scheduleVoList;
        }

        // 查询 Schedule 列表
        List<Schedule> scheduleList = scheduleMapper.selectScheduleByScheduleLineId(lineId);
        if (scheduleList == null || scheduleList.isEmpty()) {
            return scheduleVoList;
        }

        // 遍历每个 Schedule
        for (Schedule schedule : scheduleList) {
            if (schedule == null) {
                continue;
            }

            // 根据 taskId 获取 WkspTask
            WkspTask wkspTask = wkspTaskMapper.selectWkspTaskById(schedule.getTaskId());
            if (wkspTask == null || wkspTask.getOperationStatus() != 1) {
                // 仅获取 "正在生产" 状态的任务，状态码 1 代表正在生产
                continue;
            }

            // 获取产线信息
            ProductionLine productionLine = productionLineMapper.selectProductionLineByLineId(lineId);
            if (productionLine == null) {
                continue;
            }

            // 根据 progDetailId 获取 ProgPlanDetail
            ProgPlanDetail progPlanDetail = progPlanDetailMapper.selectProgPlanDetailById(wkspTask.getProgDetailId());
            if (progPlanDetail == null) {
                continue;
            }

            // 根据 progId 获取 ProgPlan
            ProgPlan progPlan = progPlanMapper.selectProgPlanById(progPlanDetail.getProgId());
            if (progPlan == null) {
                continue;
            }

            // 根据 orderNumber 获取 ProOrder 列表
            List<ProOrder> proOrders = proOrderMapper.selectProOrdersByCode(progPlan.getOrderNumber());
            if (proOrders == null || proOrders.isEmpty()) {
                continue;
            }

            // 构建并填充 ScheduleVo
            ScheduleVo vo = new ScheduleVo();
            vo.setLineName(productionLine.getLineName());
            vo.setTaskCode(wkspTask.getWkspCode());
            vo.setOrderTotal(wkspTask.getWkspPlanQuantity());
            vo.setReadyNum(wkspTask.getWkspReadyQuantity());

            // 将结果加入列表
            scheduleVoList.add(vo);
        }
        return scheduleVoList;
    }


}
